package web.servlet.logincheckcode.web;

import web.servlet.logincheckcode.dao.UserDao;
import web.servlet.logincheckcode.dao.UserDaoImpl;
import web.servlet.logincheckcode.domin.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Author: 18148
 * @Date: 2020-06-10 14:39:01
 */
@WebServlet("/register")
public class Register extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        HttpSession session = req.getSession();
        String session_code = (String) session.getAttribute("session_code");
            String username = req.getParameter("username");
            String password = req.getParameter("password");
            UserDao dao = new UserDaoImpl();
            int i = dao.register(username, password);
            if (i > 0) {
                req.getSession().setAttribute("username", username);
                String path = req.getContextPath();
                resp.sendRedirect(path + "/verification code/registeruser.jsp");
            }else {
                System.out.println("用户名重复");
            }
    }
}

